package com.yuandian.bpm.bi.modules.quartz.queue;

import com.yuandian.bpm.bi.modules.quartz.mapper.ConfReportTaskInfoMapper;
import com.yuandian.bpm.bi.modules.quartz.mapper.ConfReportTaskLogMapper;
import com.yuandian.bpm.bi.modules.quartz.util.SpringContextJobUtil;
import com.yuandian.bpm.bi.modules.quartz.vo.ConfReportTaskInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.BlockingQueue;

/**
 * 异步更新任务信息
 */
@Slf4j
public class TaskInfoStorage implements Runnable {

	private static ConfReportTaskInfoMapper taskInfoMapper;

    private final BlockingQueue<ConfReportTaskInfo> taskInfoQueue;

    public TaskInfoStorage(BlockingQueue<ConfReportTaskInfo> taskInfoQueue) { this.taskInfoQueue = taskInfoQueue; }


    @Override
    public void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {
				// 异步保存日志数据
				ConfReportTaskInfo taskInfoContent = taskInfoQueue.take();
				updateTaskInfo(taskInfoContent);
            }
        } catch (InterruptedException e) {
            // 处理线程中断异常，可记录日志或执行其他操作
            Thread.currentThread().interrupt();
        }
    }



	/**
     * 更新任务信息
	 * @param taskInfoContent	任务信息对象
	 */
    private void updateTaskInfo(ConfReportTaskInfo taskInfoContent) {
		try {
			if (taskInfoMapper == null) {
				taskInfoMapper = (ConfReportTaskInfoMapper) SpringContextJobUtil.getBean("confReportTaskInfoMapper");
			}
			taskInfoMapper.updateTaskInfo(taskInfoContent);
			log.info("[任务执行]-[任务id: {}]-更新任务信息", taskInfoContent.getId());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[任务执行]-[任务id: {}]-更新任务信息-报错", e.getMessage());
		}
    }
}
